graph = dict()
indeg = dict();

def Kahn():
    global graph
    for l in graph.values(): # compute indegree
        for ind in l:
            indeg[ind] += 1
    Q = [node for node in graph if indeg[node] == 0]
    order = []
    while Q:
        node = Q.pop() # node without incoming arrows
        order.append(node)
        for neighbor in graph[node]:
            indeg[neighbor] -= 1
            if indeg[neighbor] == 0:
                Q.append(neighbor)
    return order

# %%

def solve():
    global graph
    global indeg
    N = int(input())
    for i in range(N):
        buf = input()
        graph[buf] = list()
        indeg[buf] = 0
    M = int(input())
    for i in range(M):
        a, b = input().split(' ')
        graph[a].append(b)
    print(Kahn())

solve()
while input() == '':
    solve()
